package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;

import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowModelFileResourceHandleVo;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.utils.FileResourceManageUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 　　* @description: 文件管理控制器
 　　* @author shenrm
 　　* @date 2021/11/20 19:24
 　　*/
@RestController
@RequestMapping("/oss")
public class FlowModelFileController {

    @Autowired
    private FileResourceManageUtil fileResourceManageUtil;

    /**
     * 文件批量上传
     *
     * @param httpServletRequest
     * @param requestVo
     * @return
     */
    @ApiOperation(value = "文件批量上传")
    @ResponseBody
    @PostMapping(value = "/uploadFiles")
    public ResponseResult uploadFiles(HttpServletRequest httpServletRequest, FlowModelFileResourceHandleVo requestVo) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) httpServletRequest;
        String fileKey = "file";
        List<MultipartFile> multipartFileList = multipartHttpServletRequest.getFiles(fileKey);
        if (CollectionUtils.isEmpty(multipartFileList)) {
            return ResponseResult.fail(ErrorCode.FILE_RESOURCE_NOT_EXIST.errCode(), ErrorCode.FILE_RESOURCE_NOT_EXIST.errMessage());
        }
        // 文件上传
        return fileResourceManageUtil.fileUpload(multipartFileList, requestVo);
    }


    /**
     * 文件下载
     *
     * @param requestVo
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "文件下载")
    @ResponseBody
    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    public ResponseResult downloadFile(FlowModelFileResourceHandleVo requestVo) throws Exception{
        return fileResourceManageUtil.downLoadFile(requestVo);
    }

    /**
     *文件删除
     * @param requestVo
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "文件删除")
    @RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult delete(FlowModelFileResourceHandleVo requestVo) throws Exception {
        return fileResourceManageUtil.deleteFile(requestVo);
    }

    /**
     * 获取文件列表
     *
     * @return
     */
    @ApiOperation(value = "获取文件列表")
    @ResponseBody
    @PostMapping(value = "/getFileList")
    public ResponseResult getFileList(FlowModelFileResourceHandleVo requestVo) {
        return fileResourceManageUtil.getFileList(requestVo);
    }
}
